//
//  LLProductCell.m
//  Weibo
//
//  Created by 李灵斌 on 15-2-7.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "LLProductCell.h"
#import "LLProductItem.h"
#import "LLProductLabItem.h"
#import "LLProductSwitchItem.h"
#import "LLProductArrowItem.h"

@interface LLProductCell ()

@property (nonatomic, weak) UIView *bottomView ;

@property (nonatomic, strong) UISwitch *switchArrow;

@property (nonatomic, strong) UILabel *labText;

@property (nonatomic, strong) UIImageView *iconView;

@end

@implementation LLProductCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        UIView *bottomView = [[UIView alloc] init];
        [self.contentView addSubview:bottomView];
        self.bottomView = bottomView;
        bottomView.backgroundColor = LLColor(208, 208, 208);
        
        self.textLabel.font = [UIFont systemFontOfSize:15];
    }
    return self;
}

+ (instancetype)cellWithTableView:(UITableView *)tableView{
    
    static NSString *ID = @"cell";
    LLProductCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        
        cell = [[LLProductCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
        
    }
    return cell;
}


- (void)setItem:(LLProductItem *)item{
    
    _item = item;
    
    self.textLabel.text = item.title;
    self.detailTextLabel.text = item.subtitle;
    if (item.icon) {
        self.imageView.image = [UIImage imageNamed:item.icon];
    } else {
        self.imageView.image = nil;
    }
    
    if ([item isKindOfClass:[LLProductArrowItem class]]) {
        self.accessoryView = self.iconView;
    } else {
        self.accessoryView = nil;
    }
    
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    CGFloat margin = 0;
    self.bottomView.width = self.width - 2 * margin;
    self.bottomView.x = margin;
    self.bottomView.height = 1;
    self.bottomView.y = self.height - self.bottomView.height;
#warning 调整系统控件的子控件的frame
    self.detailTextLabel.x = CGRectGetMaxX(self.textLabel.frame) + 10;
}

#pragma mark - 懒加载 (必须强引用)
- (UISwitch *)switchArrow{
    
    if (!_switchArrow) {
        _switchArrow = [[UISwitch alloc] init];
    }
    
    return _switchArrow;
}

- (UILabel *)labText{
    
    if (!_labText) {
        _labText = [[UILabel alloc] init];
    }
    return _labText;
}

- (UIImageView *)iconView{
    
    if (!_iconView) {
        _iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"common_icon_arrow"]];
    }
    return _iconView;
}

@end
